package com.zc.kaoshi.handler;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zc.kaoshi.pojo.Kaoshi;
import com.zc.kaoshi.service.impl.KaoshiService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("kaoshi")

public class KaoshiHandler {
    @Resource
    KaoshiService kaoshiService;
    @RequestMapping("/zidian")
    @ResponseBody
    public PageInfo<Kaoshi> dataReport(@RequestParam(value = "pn",defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn,10);
//        System.out.println("分页查询控制器");
        List<Kaoshi> zidians = kaoshiService.getall();
        PageInfo<Kaoshi> pageInfo = new PageInfo<>(zidians,10); // navigatePages：一次性要显示多少页
        return pageInfo;
    }
    @RequestMapping("zidianser")
    @ResponseBody
    public List<Kaoshi> search(@RequestParam( value = "ziname") String ziname,
                               @RequestParam( value = "zitype") String zitype,
                               @RequestParam( value = "zistatu") String zistatu,
                               @RequestParam (value = "zitime1") String zitime1,
                               @RequestParam (value = "zitime2") String zitime2
    ){
        List<Kaoshi> search = kaoshiService.search(ziname, zitype, zistatu,zitime1,zitime2);
        return search;
    }
    @RequestMapping("inszidian")
    @ResponseBody
    public String inserzidian(@RequestBody Kaoshi kaoshi){
        Integer inserzidian = kaoshiService.inserzidian(kaoshi);
        return inserzidian==1?"Y":"N";
    }
    @RequestMapping("exceldownload")

    public void exceldownload(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        URLEncoder.encode("字典列表","UTF-8");
        String  fileName="zidian";
        response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
        List<Kaoshi> dataReportList = kaoshiService.getall();
        EasyExcel.write(response.getOutputStream(),Kaoshi.class).sheet("字典").doWrite(dataReportList);

    }
    @RequestMapping("zidiansave")
    @ResponseBody
    public String changezidian(@RequestBody Kaoshi kaoshi){
        Integer changezidian = kaoshiService.changezidian(kaoshi);

        return changezidian==1?"Y":"N";
    }
    @RequestMapping("zidiandel")
    public String delzidian(@RequestParam Integer id){
        Integer delzidian = kaoshiService.delzidian(id);

        return delzidian==1?"Y":"N";
    }
    @RequestMapping("delmany")
    public String delmany(@RequestBody List<Integer> ids){
        System.out.println("ids"+ids);

            int delmany = kaoshiService.delmany(ids);
            return delmany>0?"Y":"N";




    }


}
